package com.faxsun.core.web.order.security;

import org.broadleafcommerce.common.util.BLCRequestUtils;
import org.broadleafcommerce.core.order.domain.Order;
import org.broadleafcommerce.core.web.order.security.CartStateRequestProcessor;
import org.springframework.web.context.request.WebRequest;

import com.faxsun.core.order.service.type.FSOrderStatus;

public class FSCartStateRequestProcessor extends CartStateRequestProcessor{

	public Order getOverrideCart(WebRequest request) {
        Long orderId = null;
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            orderId = (Long) request.getAttribute(OVERRIDE_CART_ATTR_NAME, WebRequest.SCOPE_GLOBAL_SESSION);
        }
        Order cart = null;
        if (orderId != null) {
            cart = orderService.findOrderById(orderId);
    
            if (cart == null || 
                    cart.getStatus().equals(FSOrderStatus.SUBMITTED) || 
                    cart.getStatus().equals(FSOrderStatus.CANCELLED)) {
                return null;
            }
        }

        return cart;
    }
}
